package com.htgd.gardenexpo.controller;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.page.DictItemQuery;
import com.htgd.gardenexpo.page.DictQuery;
import com.htgd.gardenexpo.service.GeBaseDictItemService;
import com.htgd.gardenexpo.service.GeBaseDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/api/dictItem")
@Api(tags = "字典项")
public class GeBaseDictItemController {

    @Autowired
    private GeBaseDictItemService geBaseDictItemService;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @GetMapping(value = "/getItemByCode")
    @ApiOperation("根据字典code查询")
    public CommonResult<List<GeBaseDictItem>> getItemByCode(String code){
        CommonResult<List<GeBaseDictItem>> result =new CommonResult<>();

        List<GeBaseDictItem> items = geBaseDictItemService.getItemByCode(code);
        if(CollUtil.isNotEmpty(items)){
            result.setCode(200);
            result.setData(items);
        }else {
            result.setCode(300);
            result.setMessage("字典code不存在");
        }
        return result;
    }

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeBaseDictItem geBaseDictItem) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            boolean itemValueExist =geBaseDictItemService.itemValueExist(geBaseDictItem);
            //id下的value是否存在
            if(itemValueExist){
                result.setCode(300);
                result.setMessage("该字典下存在此value");
            }else {
                geBaseDictItemService.save(geBaseDictItem);
                result.setCode(200);
                result.setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }


    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeBaseDictItem geBaseDictItem) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geBaseDictItemService.updateById(geBaseDictItem);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            boolean removeById = geBaseDictItemService.removeById(id);
            if(removeById){
                result.setCode(200);
                result.setMessage("删除成功");
            }else {
                result.setCode(300);
                result.setMessage("该条不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeBaseDictItem>> list(DictItemQuery dictItemQuery) {
        CommonResult<List<GeBaseDictItem>> result = new CommonResult<>();
        try {
            PageInfo<GeBaseDictItem> list = geBaseDictItemService.pageList(dictItemQuery);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeBaseDictItem> details(String id) {
        CommonResult<GeBaseDictItem> result = new CommonResult<>();
        try {
            GeBaseDictItem geBaseDictItem = geBaseDictItemService.getById(id);
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(geBaseDictItem);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
